package ${basePackage}.api.system;

import ${basePackage}.api.BaseController;
import ${basePackage}.biz.system.SystemJobBiz;
import ${basePackage}.biz.system.SystemJobTriggerBiz;
import ${basePackage}.biz.system.dto.TriggerJobDTO;
import ${basePackage}.core.annotation.pr.PreventRepeat;
import ${basePackage}.core.annotation.trace.Trace;
import ${basePackage}.core.constants.Constants;
import ${basePackage}.core.model.ApiResponse;
import ${basePackage}.core.model.PageWrap;
import ${basePackage}.core.model.PageData;
import ${basePackage}.core.utils.Utils;
import ${basePackage}.dao.system.model.SystemJob;
import ${basePackage}.dao.system.dto.QuerySystemJobListDTO;
import ${basePackage}.dao.system.vo.SystemJobListVO;
import ${basePackage}.service.system.SystemJobService;
import ${basePackage}.service.system.SystemJobListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;    
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/job")
@Api(tags = "定时任务列表接口")
public class SystemJobController extends BaseController {

    @Autowired
    private SystemJobListService systemJobListService;
    
    @Autowired
    private SystemJobService systemJobService;

    @Autowired
    private SystemJobBiz systemJobBiz;

    @Autowired
    private SystemJobTriggerBiz systemJobTriggerBiz;

    @Trace
    @ApiOperation("手动触发")
    @PostMapping("/trigger")
    @RequiresPermissions("system:job:trigger")
    public ApiResponse triggerJob (@Validated @RequestBody TriggerJobDTO dto) {
        Utils.ThreadPool.start(() -> {
            dto.setTriggerType(Constants.Job.TriggerType.MANUAL.getCode());
            dto.setFireTime(new Date());
            systemJobTriggerBiz.trigger(dto);
        });
        return ApiResponse.success(null);
    }

    @PreventRepeat
    @ApiOperation("新建")
    @PostMapping("/create")
    @RequiresPermissions("system:job:create")
    public ApiResponse create(@Validated(Constants.OperaType.Create.class) @RequestBody SystemJob systemJob) {
        return ApiResponse.success(systemJobBiz.create(systemJob));
    }

    @ApiOperation("根据ID删除")
    @GetMapping("/delete/{id}")
    @RequiresPermissions("system:job:delete")
    public ApiResponse deleteById(@PathVariable Integer id) {
        systemJobService.deleteById(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @GetMapping("/delete/batch")
    @RequiresPermissions("system:job:delete")
    public ApiResponse deleteByIds(@RequestParam String ids) {
        systemJobService.deleteByIdInBatch(this.getIdList(ids));
        return ApiResponse.success(null);
    }

    @ApiOperation("根据ID修改")
    @PostMapping("/updateById")
    @RequiresPermissions("system:job:update")
    public ApiResponse updateById(@Validated(Constants.OperaType.Update.class) @RequestBody SystemJob systemJob) {
        systemJobBiz.updateById(systemJob);
        return ApiResponse.success(null);
    }

    @ApiOperation("修改任务状态")
    @PostMapping("/updateStatus")
    @RequiresPermissions("system:job:update")
    public ApiResponse updateStatus(@Validated(Constants.OperaType.UpdateStatus.class) @RequestBody SystemJob systemJob) {
        systemJobBiz.updateById(systemJob);
        return ApiResponse.success(null);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    @RequiresPermissions("system:job:query")
    public ApiResponse<PageData<SystemJobListVO>> findPage (@RequestBody PageWrap<QuerySystemJobListDTO> pageWrap) {
        return ApiResponse.success(systemJobListService.findPage(pageWrap));
    }

    @ApiOperation("查询所有")
    @GetMapping("/all")
    @RequiresPermissions("system:job:query")
    public ApiResponse<List<SystemJob>> findAll () {
        return ApiResponse.success(systemJobService.findList(new SystemJob()));
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    @RequiresPermissions("system:job:query")
    public ApiResponse findById(@PathVariable Integer id) {
        return ApiResponse.success(systemJobService.findById(id));
    }
}
